package com.seveneight.baijiesteward.ui.first.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.ProfitAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.ProfitBean;
import com.seveneight.baijiesteward.ui.first.contract.ProfitContract;
import com.seveneight.baijiesteward.ui.first.presenter.ProfitPresenter;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by user1 on 2018/4/4.
 */

public class ProfitListActivity extends BaseActivity implements View.OnClickListener, ProfitContract.ProfitView {

    ProfitContract.ProfitPresenter profitPresenter;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    private int page = 1;
    @BindView(R.id.recyclerview)
    LRecyclerView recyclerView;
    private ProfitAdapter profitAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_list);
    }

    @Override
    protected void initView() {
        tv_title.setText("收益明细");
        RecyclerviewInitUtil.initLineRecycler(activity, recyclerView);
        profitAdapter = new ProfitAdapter(activity);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(profitAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
    }

    @Override
    protected void initData() {
        profitPresenter = new ProfitPresenter(this);
        getData();
    }

    private void getData() {
        profitPresenter.getProfitData(activity, jsonFormat.profitList(page));
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                profitAdapter.clear();
                getData();

            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view == iv_back) {
            finish();
        }
    }

    @Override
    public void rebackProfitData(List<ProfitBean> data) {
        recyclerView.refreshComplete(data.size());
        if (page == 1 && data.size() == 0) {
            iv_empty.setVisibility(View.VISIBLE);
        } else {
            iv_empty.setVisibility(View.GONE);
        }
        if (data.size() == 0) {
            recyclerView.setNoMore(true);
        }
        profitAdapter.addAll(data);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }
}
